"""
管理员功能的实现
"""

from src import register
from db import db_handler


def add_account() -> None:
    """添加账户"""
    register()


def modify_balance() -> None:
    """修改余额"""
    while True:
        username = input('请输入用户名：').strip()
        user_dict = db_handler.select(username)

        if user_dict is None:
            print('该用户不存在，请重新输入！')
            continue

        while True:
            sum = input('请输入目标金额：').strip()
            if not sum.isdigit():
                print('输入的金额有误，请重新输入')
                continue
            sum = int(sum)

            user_dict['balance'] = sum
            db_handler.save(username, user_dict)
            print('余额修改成功！')
            return


def frozen_account() -> None:
    """冻结账户"""
    while True:
        username = input('请输入用户名：').strip()
        user_dict = db_handler.select(username)

        if user_dict is None:
            print('该用户不存在，请重新输入！')
            continue

        # 冻结账户
        user_dict['locked'] = True
        db_handler.save(username, user_dict)
        print(f'账户{username}已被冻结！')
        return


def unfreezing_accounts() -> None:
    """解冻账户"""
    while True:
        username = input('请输入用户名：').strip()
        user_dict = db_handler.select(username)

        if user_dict is None:
            print('该用户不存在，请重新输入！')
            continue

        # 冻结账户
        user_dict['locked'] = False
        db_handler.save(username, user_dict)
        print(f'账户{username}已被解冻！')
        return
